﻿#include "FMenu.h"
#include "../Base/FTheme.h"

#include <QGraphicsDropShadowEffect>


FMenu::FMenu(QWidget *parent)
    : FMenu("", parent)
{
}

FMenu::FMenu(const QString &text, QWidget *parent)
    : QMenu(text, parent)
{
    // 允许透明、无边框、禁用系统阴影
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint | Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setAttribute(Qt::WA_StyledBackground, true);
    setAttribute(Qt::WA_StyleSheet, true);
    setAttribute(Qt::WA_NoSystemBackground, true);


    // 基础样式（圆角 + 背景 + 分隔线 + hover）
    setStyleSheet(fstylesheet());

    // 阴影
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setBlurRadius(5);
    shadow->setColor(FColors::getBaseColor(FColors::Info));
    shadow->setOffset(0, 0);
    // setGraphicsEffect(shadow);  // 牺牲阴影效果解决图层错误：
    // UpdateLayeredWindowIndirect failed for ptDst=(1603, 113), size=(273x102), dirty=(283x112 -5, -5) (参数错误。)
}

QString FMenu::fstylesheet()
{
    return QString(R"(
QMenu {
    background: #fbfbfb;
    border: 1px solid %1;
    border-radius: 4px;
    padding: 6px 6px;
    margin: 5px;
}
QMenu::icon {
    padding-left: 12px;
}
QMenu::item {
    border-radius: 3px;
    margin: 2px 0px;
    padding: 6px 15px;
    background: transparent;
}
QMenu::item:selected {
    background: %2;
}
QMenu::item:disabled {
    color: %3;
    background: transparent;
}
QMenu::separator {
    height: 1px;
    margin: 4px 0px;
    background: %4;
}
)").arg(FColors::getHoverColor(FColors::Default).name(QColor::HexArgb),
        FColors::getBaseColor(FColors::Default).name(QColor::HexArgb),
        FColors::getHoverColor(FColors::Default).name(QColor::HexArgb),
        FColors::getHoverColor(FColors::Default).name(QColor::HexArgb));
}
